//
//  ECMineSafeFirstSetPasswordVc.m
//  Edu_iOS2018
//
//  Created by apple on 2018/3/5.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#define Textfield_height        (960*0.1406)         //封装输入框的高度

#import "ECMineSafeFirstSetPasswordVc.h"
#import "ECUITextField_Account.h"
#import "ECRegularExpression.h"

@interface ECMineSafeFirstSetPasswordVc (){
    ECUITextField_Account *uitf_newPassword;
    ECUButton *sureBtn;
}

@end

@implementation ECMineSafeFirstSetPasswordVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"新密码设置";
    
    uitf_newPassword = [[ECUITextField_Account alloc]initWithFrame:CGRectMake(kProportion_Width(60), kProportion_Width(60), kScreenWidth - kProportion_Width(MarginLeft_Textfield*2) , kProportion_Height(Textfield_height)) placeholder:@"登录密码(6-20位英文/数字/符号)" leftImage:[UIImage imageNamed:@"pwd_user"]];
    [self.view addSubview:uitf_newPassword];
    uitf_newPassword.textField.secureTextEntry = YES;
    
    // login btn
    sureBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorNormal"] forState:UIControlStateNormal];
    sureBtn.frame = CGRectMake(kProportion_Width(60), CGRectGetMaxY(uitf_newPassword.frame) + kProportion_Height(MarginHeight_textfield), kScreenWidth - kProportion_Width(60*2),(kScreenWidth - kProportion_Width(60*2))* 0.1406);
    [sureBtn addTarget:self action:@selector(action_Login) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"flatButton_colorSelected"] forState:UIControlStateHighlighted];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:ceil(20)];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:sureBtn];
}

-(void)action_Login{
    if (![ECRegularExpression detectionIsPasswordQualified:uitf_newPassword.textField.text]) {
        [MBProgressHUD showTipMessageInWindow:kAlertTipOfPasswordValid];
        return;
    }
    [self getDataWithURL];
}

/*  请求数据  */
-(void)getDataWithURL{
    DLog(@"%@",[[UserInfoManager shareUser] token]);
    [HWHttpTool requestWithMethod:@"POST"
                              url:URL_POST_SetPassword
                           params:@{@"password":uitf_newPassword.textField.text
                                    }
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              DLog(@"%@",json);
                              [MBProgressHUD showTipMessageInWindow:@"密码设置成功"];
                              [self.navigationController popViewControllerAnimated:YES];
                          }
                          failure:^(NSError *error) {
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              DLog(@"%@",str);
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:str];
                              }
                          }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
